How to represent a Board Panel in Java for a game ? [+code]

Posted by FILIaS on Stack Overflow See other posts from Stack Overflow or by FILIaS
Published on 2011-01-14T18:52:43Z Indexed on 2011/01/14 22:53 UTC
Read the original article Hit count: 312

Filed under:
|
|
|

I wanna fix a 2D board for a game. I've already fixed other panels for the Gui and everything goes well. But the panel for the board cant be printed on the window. I'm a bit confused about it as i think i've followed the same ideas as for the others panels i need.

Here's what i've done:

EDIT:*EDIT* what i'm trying to do is fix a board panel for the game according to the dimensions of the it,hold every square in an array in order to use it after wherever it;s needed. I draw each little square of it with the method draw and put it back to the panel. So, each square on the board is a panel. This is the idea. But as u can see. There are troubles/errors on it.

EDIT: code updated. just found a part of the problem. i thought first that i had set background to squared, but i didnt. with this one it appears on the panel a wide black "column". Unfortunately,still none squares. :(

One More EDIT: Also,i realized that draw method is never called. when i put the draw method in the following method i can see the squares but they remain small. I redefine them with setSize but still no change.

/**
    *Method used to construct the square in the area of the
    *gui's grid. In this stage a GUISquare array is being constructed, 
    * used in the whole game as
    *a mean of changing a square graphical state.
    *@param squares is the squares array from whom the gui grid will be
    *constructed.
    *@see getSquare about the correspondance beetween a squareModel and
    * a GUISquare.
    */
    private void initBoardPanel(SquareModel[][] squares){

         BoardPanel.setLayout(new GridLayout(height ,width )); //set layout

         SquareRenderer[][] Squares;
         JPanel[][] grid;
         Squares=new GUISquare[height][width()];
         grid=new JPanel[height()][width()];

         for (int i=0; i<height(); i++){
                for (int j=0; j<width() ; j++){
                    grid[i][j] = new JPanel( );
                SquareRenderer kout=new SquareRenderer(i,j);
                koutaki.setSquare(myGame.getSquares()[i][j]);

                if (myGame.getSquares()[i][j] instanceof SimpleSquareModel){
                        kout.draw(i,j,"");}
                else
                {       kout.draw(i,j);
                }
                kout.setVisible(true);
                kout.setBackground(Color.BLACK);
                kout.setSize(50,50);

                Squares[i][j]= kout;

                grid[i][j].setSize(50,50);
                grid[i][j].setVisible(true);
                grid[i][j].setBackground(Color.BLACK);

                BoardPanel.add(kout);
                BoardPanel.setVisible(true);
                BoardPanel.setBackground(Color.WHITE); 
                }
        }
        this.add(BoardPanel,BorderLayout.WEST);
     //   this.pack(); //sets appropriate size for frame
        this.setVisible(true); //makes frame visible
}







IMPLEMENTED BY SQUARERENDERER:
            /**
 * Transformer for Snake/Ladder
 * <br>This method is used to display a square on the screen.
 */
public void draw(int i,int j) {
    JPanel panel = new JPanel();
    panel.setLayout(new BorderLayout());
    JLabel label1 = new JLabel("Move To"+myGame.getSquares()[i][j].getGoTo());
    JLabel label2 = new JLabel(""+myGame.getSquares()[i][j].getSquare());
    JSeparator CellSeparator = new JSeparator(orientation);
    panel.add(CellSeparator);
    panel.setForeground(Color.ORANGE);
    panel.add(label2, BorderLayout.NORTH);
    panel.add(label1, BorderLayout.CENTER);
}

© Stack Overflow or respective owner

Related posts about java

Related posts about gui